/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Entity.NhaXuatBan;
import Util.DatabaseHandle;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Dark_Knight
 */
public class NhaXuatBanImpl implements NhaXuatBanDAO {

    public boolean add(NhaXuatBan data) {
        String sql = "{call sp_NHAXUATBAN_add(?,?,?,?,?)}";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        
        try {
            db.setOutParameter(1, java.sql.Types.INTEGER);
            db.setParameter(2, data.getTenNXB());
            db.setParameter(3, data.getDiaChiNXB());
            db.setParameter(4, data.getDienThoaiNXB());
            db.setParameter(5, data.getEmailNXB());
            
            db.executeStorePro();
            
            data.setMaNXB(db.getIntOutValue(1));
            db.closed();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(NhaXuatBanImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public boolean delete(int MaNhaXuatBan) {
        String sql = "{call sp_NHAXUATBAN_edit(?,?,?,?,?)}";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        
        try {
            db.setParameter(1, MaNhaXuatBan);
            db.excuteQuery();
            db.closed();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(NhaXuatBanImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public boolean edit(NhaXuatBan data) {
        String sql = "{call sp_NHAXUATBAN_edit(?,?,?,?,?)}";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        
        try {
            db.setParameter(1, data.getMaNXB());
            db.setParameter(2, data.getTenNXB());
            db.setParameter(3, data.getDiaChiNXB());
            db.setParameter(4, data.getDienThoaiNXB());
            db.setParameter(5, data.getEmailNXB());
            
            db.executeStorePro();
            db.closed();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(NhaXuatBanImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }
    
    public ResultSet getAll() {
        String sql = "{call sp_NHAXUATBAN_getAll()}";
        DatabaseHandle db = new DatabaseHandle(sql, false);
        ResultSet rs = db.excuteQuery();
    //    db.closed();
        return rs;
    }

    public ResultSet getByID(int id) {
        String sql = "{call sp_NHAXUATBAN_getByID(?)}";
        DatabaseHandle db = new DatabaseHandle(sql, false);
        ResultSet rs = null;
        try {
            db.setParameter(1, id);
            rs = db.excuteQuery();
        } catch (SQLException ex) {
            Logger.getLogger(NhaXuatBanImpl.class.getName()).log(Level.SEVERE, null, ex);            
        }   
    //    db.closed();
        return rs;
    }
}
